
// <copyright file="StoreFlushEventArgs.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The store flush event args.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Concurrent
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    /// <summary>
    /// The store flush event args.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public sealed class StoreFlushEventArgs<T> : EventArgs, IEnumerable<T>
    {
        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="StoreFlushEventArgs{T}"/> class.
        /// </summary>
        /// <param name="storeData">
        /// The store data.
        /// </param>
        public StoreFlushEventArgs(T[] storeData)
            : this(storeData, 0, storeData.Length - 1)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StoreFlushEventArgs{T}"/> class.
        /// </summary>
        /// <param name="storeData">
        /// The store data.
        /// </param>
        /// <param name="fromIndex">
        /// The from index.
        /// </param>
        /// <param name="toIndex">
        /// The to index.
        /// </param>
        public StoreFlushEventArgs(T[] storeData, int fromIndex, int toIndex)
        {
            this.StoreData = storeData;
            this.FromIndex = fromIndex;
            this.ToIndex = toIndex;
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the length.
        /// </summary>
        public int Length
        {
            get
            {
                return this.ToIndex - this.FromIndex + 1;
            }
        }

        #endregion Public Properties

        #region Properties

        /// <summary>
        /// Gets the from index.
        /// </summary>
        internal int FromIndex { get; private set; }

        /// <summary>
        /// Gets the store data.
        /// </summary>
        internal T[] StoreData { get; private set; }

        /// <summary>
        /// Gets the to index.
        /// </summary>
        internal int ToIndex { get; private set; }

        #endregion Properties

        #region Public Indexers

        /// <summary>
        /// The this.
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public T this[int index]
        {
            get
            {
                return this.StoreData[index + this.FromIndex];
            }
        }

        #endregion Public Indexers

        #region Public Methods and Operators

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        public IEnumerator<T> GetEnumerator()
        {
            for (int i = this.FromIndex; i <= this.ToIndex; i++)
            {
                yield return this.StoreData[i];
            }
        }

        /// <summary>
        /// The sort.
        /// </summary>
        /// <param name="comparer">
        /// The comparer.
        /// </param>
        public void Sort(IComparer<T> comparer)
        {
            Array.Sort(this.StoreData, this.FromIndex, this.ToIndex - this.FromIndex + 1);
        }

        #endregion Public Methods and Operators

        #region Explicit Interface Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            for (int i = this.FromIndex; i <= this.ToIndex; i++)
            {
                yield return this.StoreData[i];
            }
        }

        #endregion Explicit Interface Methods
    }
}